package com.shine.commonlibrary.utils.testpackage;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.shine.commonlibrary.R;
import com.shine.commonlibrary.utils.base.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * Created by M_genius on 2017/9/7 16:08.
 * 描述：
 */

public class FragmentTwo extends BaseFragment {

    private static FragmentTwo mInstance;
    private TextView tvTwo;
    private Button btOne;
    private Button btTwo;

    public static FragmentTwo getInstance() {
        if (mInstance == null) {
            mInstance = new FragmentTwo();
        }
        return mInstance;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_two;
    }

    @Override
    protected void loadData() {
        super.loadData();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        tvTwo = (TextView) view.findViewById(R.id.tvTwo);

        btOne = (Button) view.findViewById(R.id.btOne);
        btOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEventOne("Hello everyone!"));
            }
        });
        btTwo = (Button) view.findViewById(R.id.btTwo);
        btTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEventTwo("Hello everyone!"));
            }
        });
        return view;
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    @Subscribe
    public void onMessageEventOne(MessageEventOne event){
        tvTwo.setText("收到事件MessageEventOne");
    }

    @Subscribe
    public void onMessageEventTwo(MessageEventTwo event){
        tvTwo.setText("收到事件MessageEventTwo");
    }
    public void clickOne(View view){
        EventBus.getDefault().post(new MessageEventOne("Hello everyone!"));
    }

    public void clickTwo(View view){
        EventBus.getDefault().post(new MessageEventTwo("Hello everyone!"));
    }

}
